package orxatas.travelme.manager;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import orxatas.travelme.activity.AsyncActivity;
import orxatas.travelme.databases.exceptions.PhotoNoInLocal;
import orxatas.travelme.entity.Photo;
import orxatas.travelme.manager.exceptions.CantLoadPhotoFromFile;
import orxatas.travelme.sync.AsyncNoticeCode;
import orxatas.travelme.sync.SyncPhotos;

public class PhotoManager {

	private AsyncActivity asyncActivity;
	
	private SyncPhotos syncPhotos = null;

	public PhotoManager(AsyncActivity asyncActivity){
		this.asyncActivity=asyncActivity;
		
		syncPhotos = new SyncPhotos(this, asyncActivity);
	}
	
	/**
	 * Read a file and load it as a bitmap.
	 * 
	 * @param path Local path to the bitmap.
	 * @throws CantLoadPhotoFromFile If the file is impossible to load.
	 * */
	public Bitmap path2Bitmap(String path) throws CantLoadPhotoFromFile{
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inTempStorage = new byte[16*1024];
		options.inSampleSize = 4;
		
		Bitmap bMap = BitmapFactory.decodeFile(path, options);
		
		if(bMap == null)
			throw new CantLoadPhotoFromFile();
		
		return bMap;
	}

	public Photo getPhoto(int idPhoto) throws PhotoNoInLocal{

		return syncPhotos.getPhoto(idPhoto);
	}
}
